﻿using System.IO;
using EGConfig;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;


public class ExcelConfigEditor : EditorWindow
{
    public static string outputClassDirPath = null;
    public static string outputJsonDirPath = null;
    public static string outputLuaDirPath = "LuaFramework/Lua/Config";

    [MenuItem("Assets/生成C#类")]
    public static void CreateCSharpConfigData()
    {
        foreach (var obj in Selection.objects)
        {
            string path = GetObjectPath(obj);
            var configClass = GetConfigClass(path);
            string outputFilePath = $"{Application.dataPath}/{outputClassDirPath}/{configClass.Data.upperName}.cs";
            string text = configClass.GetClassText<CSharpClassTemplate>();
            CreateFile(outputFilePath, text);

            Debug.Log(configClass.Data.upperName);
        }

        AssetDatabase.Refresh();
        Debug.Log("生成完成！");
    }

    [MenuItem("Assets/生成Json表")]
    public static void CreateJsonTableConfigData()
    {
        foreach (var obj in Selection.objects)
        {
            string path = GetObjectPath(obj);
            var configClass = GetConfigClass(path);
            string outputFilePath = $"{Application.dataPath}/{outputJsonDirPath}/{configClass.Data.upperName}.json";
            string text = configClass.GetTableText<JsonTableTemplate>();
            CreateFile(outputFilePath, text);

            Debug.Log(configClass.Data.upperName);
        }

        AssetDatabase.Refresh();
        Debug.Log("生成完成！");
    }

    [MenuItem("Assets/生成Lua表")]
    public static void CreateLuaTableConfigData()
    {
        foreach (var obj in Selection.objects)
        {
            string path = GetObjectPath(obj);
            var configClass = GetConfigClass(path);
            string outputFilePath = $"{Application.dataPath}/{outputLuaDirPath}/{configClass.Data.lowerName}.lua";
            string text = configClass.GetTableText<LuaTableTemplate>();
            CreateFile(outputFilePath, text);

            Debug.Log(configClass.Data.upperName);
        }

        AssetDatabase.Refresh();
        Debug.Log("生成完成！");
    }
    
    

    [MenuItem("Assets/生成C#类", true)]
    [MenuItem("Assets/生成Json表", true)]
    [MenuItem("Assets/生成Lua表", true)]
    public static bool CheckType()
    {
        foreach (var obj in Selection.objects)
        {
            string path = GetObjectPath(obj);
            string ext = Path.GetExtension(path);
            if (ext.ToLower() != ".xlsx")
            {
                return false;
            }
        }

        return true;
    }

    private static string GetObjectPath(Object obj)
    {
        string path = AssetDatabase.GetAssetPath(obj);
        path = $"{Application.dataPath}/../{path}";
        return path;
    }

    private static ConfigClass GetConfigClass(string path)
    {
        var configData = ExcelAnalysis.GetConfigData(path);
        return new ConfigClass(configData);
    }

    private static void CreateFile(string path, string text)
    {
        var fs = File.Create(path);
        var streamWriter = new StreamWriter(fs);
        streamWriter.Write(text);
        streamWriter.Flush();
        streamWriter.Close();
    }
}